package com.futureweaver.service.impl;

import com.futureweaver.domain.MedicalRecord;
import com.futureweaver.domain.MedicalRecordDisease;
import com.futureweaver.mapper.MedicalRecordDiseaseMapper;
import com.futureweaver.mapper.MedicalRecordMapper;
import com.futureweaver.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Autowired
    private MedicalRecordMapper mapper;

    @Autowired
    private MedicalRecordDiseaseMapper medicalRecordDiseaseMapper;

    @Override
    @Transactional
    public boolean insert(MedicalRecord medicalRecord) {
        int insertResult = mapper.insert(medicalRecord);

        for (MedicalRecordDisease disease : medicalRecord.getDiseases()) {
            disease.setMedicalRecordId(medicalRecord.getId());
            disease.setDiseaseId(disease.getId());
        }

        int diseaseInsertResult = medicalRecordDiseaseMapper.insert(medicalRecord.getDiseases());

        return true;
    }

    @Override
    public boolean update(MedicalRecord medicalRecord) {
        return mapper.update(medicalRecord) == 1;
    }
}
